/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');



    app.useModule("ui.table");

    var toastr =require('toastr');

    var datepicker = require('datepicker');

    app.controller('roleCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.orderParams = {
            url: "/admin/role/list"
        };
        datepicker($scope);
        $scope.ctrlScope=$scope;
        $scope.search={};
        $scope.roleList={};
        /* $scope.showModal = false;*/
        $scope.enableUpload=false;
        $scope.isEdit=false;
        $scope.data={};
        $scope.checked=[];
        $scope.resources=[];
        var i =0;
        $scope.n = true;
        $scope.b = false;

        //创建
        $scope.addOrderModal=function(){
            $scope.isEdit=false;
            $scope.ctrlScope.list={};
            $scope.orderModal = !$scope.orderModal;
            /* $scope.seeInformation(id);
             $scope.openEditModal(id);*/
            $scope.resource();
        };

        //全局查询
        $scope.search = function() {
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };

        //全局查询重置
        $scope.reset=function(){
            $scope.orderParams.ajaxParam={};
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.reload();
        };
        //修改
        $scope.openViewOrderModal=function(id){
            $scope.isEdit=true;
            $scope.orderModal = !$scope.orderModal;
            $scope.seeInformation(id);
            $scope.openEditModal(id);
        }

        $scope.seeInformation=function(id){
            $http({
                method : 'POST',
                url :"/admin/role/getOne",
                data: {
                    id:id
                }
            }).success(function(data) {
                console.info(data);
                $scope.ctrlScope.list = data.role;
            }).error(function(data, status, headers, config) {
            });

            //获取编辑信息
            $scope.openEditModal=function(id){
                $scope.roleAddModal = !$scope.roleAddModal;
                $scope.isEdit=true;
                for(var i=0;i<$scope.checked.length;i++){
                    $scope.checked[i]=false;
                }
                var param = {
                    "id":id,
                }
                $http.post("/admin/role/getOne", param)
                    .success(function(data){
                        console.info(1);

                        console.info(data);
                        for(var i=0;i<data.resource.length;i++){
                            $scope.checked[data.resource[i].sourceId]=true;
                        }
                        // $scope.ctrlScope.id=data.role.id;
                        /*     $scope.ctrlScope.rolename=data.role.roleName;
                         $scope.ctrlScope.description=data.role.description;
                         */
                    }).error(function(){
                    });
            }
            $scope.resource();
        };

        //显示所有复选框
        $scope.resource=function(){
            $http.post("/admin/resource/getList")
                .success(function(data){
                    var sOne=new Array();
                    var sTwo=new Array();

                    for(var i=0;i<data.length;i++){
                        if(data[i].parentId == 0){
                            $scope.checked[data[i].id]=false;
                            $scope.resources[data[i].id]=data[i];
                            sOne.push(data[i]);
                        }
                    }

                    for(var i=0;i<sOne.length;i++){
                        var itemArray=new Array();
                        for(var j=0;j<data.length;j++){
                            if(data[j].parentId == sOne[i].id){
                                $scope.checked[data[j].id]=false;
                                $scope.resources[data[j].id]=data[j];
                                itemArray.push(data[j]);
                            }
                        }
                        sTwo.push(itemArray);
                    }
                    console.info(sOne);
                    console.info(sTwo);
                    $scope.resourceData=sOne;
                    $scope.subResourceData=sTwo;
                }).error(function(){

                });
        };

        $scope.checkAll=function(resource){
            var onoff=false;
                $http.post("/admin/resource/getList")
                    .success(function(data){
                        console.info($scope.resourceData);
                            for(var i=0;i<data.length;i++){
                                if($scope.checked[resource.id]){
                                    if(data[i].parentId == resource.id){
                                        $scope.checked[data[i].id]=true;
                                    }
                                }
                                else{
                                    if(data[i].parentId == resource.id){
                                        $scope.checked[data[i].id]=false;
                                    }
                                }
                            }

                    }).error(function(){

                    });

        };
        //添加编辑
        $scope.addRole=function(){
            var all=null;
            var s="";
            angular.forEach($scope.checked, function(value, key) {
                if(value==true){
                    s=s+key+",";
                }
            });

            var param={
                sources:s,
                roleName:$scope.ctrlScope.list.roleName,
                description:$scope.ctrlScope.list.description,
                id:$scope.ctrlScope.list.id
            };
            console.log(s);
            var url="/admin/role/";
            if($scope.isEdit){
                url=url+"modify";
            }else{
                url=url+"add";
            }
            $http.post(url, param)
                .success(function(data){
                    if(data.result=='success'){
                        toastr.success('提交成功');
                        $scope.orderModal = !$scope.orderModal;
                        $scope.orderParams.reload();
                    }else{
                        toastr.error('提交失败')
                    }

                }).error(function(data){

                });
        }
    }]);


});